package com.wanhe.base.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @param <T>
 * @author chaoxunYang
 * @ClassName: LiveRoomBaseAdapter
 * @Description: 适配器的基类
 * @date 2017年06月27日 上午10:33:04
 */
public abstract class TBaseAdapter<T> extends BaseAdapter {

    protected  int mItemLayoutId;
    protected Context mContext;
    protected List<T> mDatas;
    protected View itemView;

    public TBaseAdapter(Context context, int itemLayoutId) {
        this.mContext = context;
        this.mItemLayoutId = itemLayoutId;
        this.mDatas = new ArrayList<T>();
    }

    public TBaseAdapter(Context context, View itemView) {
        this.mContext = context;
        this.itemView = itemView;
        this.mItemLayoutId = 0;
        this.mDatas = new ArrayList<T>();
    }

    public TBaseAdapter(Context context) {
        this.mContext = context;
        this.mDatas = new ArrayList<T>();
    }

    @Override
    public int getCount() {
        return this.mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return this.mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return (long) position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AdapterViewHolder viewHolder;
        if (this.mItemLayoutId == 0) {
            viewHolder = AdapterViewHolder.dequeueReusableAdapterViewHolder(convertView, parent,
                    itemView);
        } else {
            viewHolder = AdapterViewHolder.dequeueReusableAdapterViewHolder(convertView, parent,
                    mItemLayoutId);
        }

        this.fillData(viewHolder.getViewHolderHelper(), position, this.getItem(position));

//        if(getItemView(position)!=null){
//			return getItemView(position);
//		}
        return viewHolder.getConvertView();
    }

    protected abstract void fillData(ViewHolderUtil var1, int var2, T var3);

    public List<T> getDatas() {
        return this.mDatas;
    }

    public void addNewDatas(List<T> datas) {
        if (datas != null) {
            this.mDatas.addAll(0, datas);
            this.notifyDataSetChanged();
        }

    }

    public void addMoreDatas(List<T> datas) {
        if (datas != null) {
            this.mDatas.addAll(this.mDatas.size(), datas);
            this.notifyDataSetChanged();
        }

    }

    public void setDatas(List<T> datas) {
        if (datas != null) {
            this.mDatas = datas;
        } else {
            this.mDatas.clear();
        }

        this.notifyDataSetChanged();
    }

    public void clear() {
        this.mDatas.clear();
        this.notifyDataSetChanged();
    }

    public void removeItem(int position) {
        this.mDatas.remove(position);
        this.notifyDataSetChanged();
    }

    public void removeItem(T model) {
        this.mDatas.remove(model);
        this.notifyDataSetChanged();
    }

    public void removeAll(List<T> list) {
        this.mDatas.removeAll(list);
        this.notifyDataSetChanged();
    }

    public void addItem(int position, T model) {
        this.mDatas.add(position, model);
        this.notifyDataSetChanged();
    }

    public void addFirstItem(T model) {
        this.addItem(0, model);
    }

    public void addLastItem(T model) {
        this.addItem(this.mDatas.size(), model);
        this.notifyDataSetChanged();
    }

    public void setItem(int location, T newModel) {
        this.mDatas.set(location, newModel);
        this.notifyDataSetChanged();
    }

    public void setItem(T oldModel, T newModel) {
        this.setItem(this.mDatas.indexOf(oldModel), newModel);
    }

    public void moveItem(int fromPosition, int toPosition) {
        Collections.swap(this.mDatas, fromPosition, toPosition);
        this.notifyDataSetChanged();
    }

//	public  View getItemView(int possition){
//		return null;
//	}
}
